package com.smart.controller;

import com.alibaba.fastjson.JSON;
import com.smart.common.ResultVo;
import com.smart.entity.Article;
import com.smart.entity.Article_Class;
import com.smart.entity.Article_List;
import com.smart.entity.Label;
import com.smart.service.ArticleService;
import com.smart.service.ClassService;
import com.smart.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminAmsController {
    @Autowired
    ArticleService articleService;

    @Autowired
    ClassService classService;

    @Autowired
    LabelService labelService;

    @PostMapping("/ams/write")
    public ResultVo ArticleWrite(@RequestParam Map<String, String> temp) {
        Integer result = articleService.ArticleWrite(temp);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/delete")
    public ResultVo ArticleDelete(Integer id) {
        Integer result = articleService.ArticleDelete(id);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/all")
    public ResultVo ArticleAll(Integer currentPage,Integer pageSize) {
        Map<String, Object> articles = articleService.ArticleAll(currentPage, pageSize);
        return ResultVo.ok().data("item", articles);
    }

    @GetMapping("/ams/list")
    public ResultVo ArticleList(Integer currentPage,Integer pageSize) {
        Map<String, Object> articles = articleService.ArticleList(currentPage, pageSize);
        return ResultVo.ok().data("item", articles);
    }

    @GetMapping("/ams/class")
    public ResultVo ArticleClass(Integer currentPage,Integer pageSize) {
        Map<String, Object> article_classes = classService.ArticleClassAll(currentPage, pageSize);
        return ResultVo.ok().data("item", article_classes);
    }

    @GetMapping("/ams/classadd")
    public ResultVo ArticleClassAdd(String classname) {
        int result = classService.ArticleClassAdd(classname);
        return ResultVo.ok().data("item", result);
    }


    @GetMapping("/ams/classdelete")
    public ResultVo ArticleClassDetle(Integer id) {
        if (id.equals(1)){
            String msg = "不可删除！";
            return ResultVo.error().data("item", msg);
        }
        int result = classService.ArticleClassDelete(id);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/byclass")
    public ResultVo ArticleByClass(Integer id) {
        System.out.println(id);
        Article_Class result = classService.ArticleByClass(id);
        return ResultVo.ok().data("item", result);
    }

    @PostMapping("/ams/classedit")
    public ResultVo ArticleClassEdit(Article_Class temp) {
        Integer result = classService.ArticleClassEdit(temp);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/label")
    public ResultVo ArticleLabel(Integer currentPage,Integer pageSize) {
        Map<String, Object> result = labelService.ArticleLabelAll(currentPage, pageSize);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/labeladd")
    public ResultVo ArticleLabelAdd(String labelname) {
        int result = labelService.ArticleLabelAdd(labelname);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/labeldelete")
    public ResultVo ArticleLabelDetle(Integer id) {
        if (id.equals(1)){
            String msg = "不可删除！";
            return ResultVo.error().data("item", msg);
        }
        int result = labelService.ArticleLabelDetle(id);
        return ResultVo.ok().data("item", result);
    }

    @GetMapping("/ams/bylabel")
    public ResultVo ArticleByLabel(Integer id) {
        Label result = labelService.ArticleByLabel(id);
        return ResultVo.ok().data("item", result);
    }

    @PostMapping("/ams/labeledit")
    public ResultVo ArticleLabelEdit(Label label) {
        Integer result = labelService.ArticleLabelEdit(label);
        return ResultVo.ok().data("item", result);
    }

}
